<!-- 资产统计分析-按路段分类 -->
<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { ref, unref, reactive } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { Search } from '@/components/Search'
import { Detail } from './components'
import { Dialog } from '@/components/Dialog'
import { busFacilityPageDataList } from '@/api/highway'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { BaseButton } from '@/components/Button'

const { t } = useI18n()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const res = await busFacilityPageDataList(
      {
        pageNum: unref(currentPage),
        pageSize: unref(pageSize)
      },
      {
        ...unref(searchParams)
      }
    )
    return {
      list: res.data.records || [],
      total: res.data.total || 0
    }
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList } = tableMethods
const marginR = '25px'

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('userDemo.index'),
    search: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      type: 'index'
    }
  },
  {
    field: 'road',
    label: '所属路段'
  },
  {
    field: 'quantity',
    label: '资产数量',
    search: {
      hidden: true
    }
  },
  {
    field: 'online',
    label: '在用资产',
    search: {
      hidden: true
    }
  },

  {
    field: 'onlineRate',
    label: '资产在线率',
    search: {
      hidden: true
    }
  },
  {
    field: 'action',
    label: t('userDemo.action'),
    detail: {
      hidden: true
    },
    search: {
      hidden: true
    },
    table: {
      width: 60,
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <BaseButton type="success" link onClick={() => action(row)}>
                {t('exampleDemo.detail')}
              </BaseButton>
            </>
          )
        }
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const searchParams = ref({})
const setSearchParams = (params: any) => {
  console.log('params', params)

  currentPage.value = 1
  if (params.createTime && params.createTime.length === 2) {
    params = { ...params, startTime: params.createTime[0], endTime: params.createTime[1] }
  }
  console.log('params', params)
  searchParams.value = params
  getList()
}

const action = (row: any) => {
  dialogTitle.value = t('exampleDemo.detail')
  currentRow.value = row
  dialogVisible.value = true
}

const dialogVisible = ref(false)
const dialogTitle = ref('')

const currentRow = ref<any>()
</script>

<template>
  <ContentWrap class="flex-[1]">
    <Search :schema="allSchemas.searchSchema" @reset="setSearchParams" @search="setSearchParams" />

    <Table
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      @register="tableRegister"
      :pagination="{
        total
      }"
    />
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <Detail :detail-schema="allSchemas.detailSchema" :current-row="currentRow" />
    <template #footer>
      <BaseButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</BaseButton>
    </template>
  </Dialog>
</template>
